import 'package:flutter/cupertino.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';

import 'base_state_manager.dart';
///https://blog.csdn.net/qq_45104740/article/details/122992045
///flutter 不支持匿名类，所以用函数的办法解决

class BaseLvAdapter<T> extends BaseStateManager {

  List<T>? _ts;

  Widget? Function(T t, int index) onItemCreateAndDataBind;

  List<T>? get ts  => _ts;

  BaseLvAdapter(this.onItemCreateAndDataBind);

  void setData(List<T>? ts){
    if (ts != null) {
      _ts = ts ;
    }
  }

  void setDataAndNotify(List<T>? ts) {
    if (ts != null) {
      _ts = ts ;
      update();
    }
  }

  void appendDataAndNotify(List<T>? ts) {
    if(ts == null) return;

    if((_ts != null)){
      _ts?.addAll(ts);
    }else{
      _ts = ts ;
    }
    update();
  }

  void clearDataAndNotify() {
    _ts?.clear();
    update();
  }

  void notifyDataSetChanged() {
    update();
  }

  int getItemCount() {
    return (_ts == null) ? 0 : _ts!.length;
  }

  bool isEmpty() {
    if(_ts == null){
      return true;
    }else {
      return _ts!.isEmpty;
    }
  }

  T? getItem(int position) {
    if ((position >= 0) && (position < _ts!.length)) {
      return _ts?[position];
    }
    return null;
  }

  ///更新某一条数据
  void updateItem(int position){
    if ((_ts != null) && (position >= 0) && (position < _ts!.length)) {
      update([position]);
    }
  }

}
